home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-02-20 | 9.1 KB | 299 lines | [TEXT/CWIE] |
- /*
- File: Object.c
-
- Contains: base class for many other classes
-
- Written by: Bruce Horn, Steve Capps, Larry Kenyon, John Meier, scott douglass, Darin Adler,
- Paul Mercer, Bryan Stearns, Dave Owens
-
- Andy Nicholas
-
- Copyright: © 1988-1995 by Apple Computer, Inc., all rights reserved.
-
- <5> 6/23/95 ga
- */
-
- #ifdef MWTRACEBACKTABLES
- #pragma traceback on
- #endif
-
- #include "Object.h"
- #include "Exceptions.h"
- #include "Behavior.h"
-
- //
- // For CopyMemory
- //
- #include "AbstractData.h"
-
-
- //========================================================================================
- // CLASS TClassData
- //========================================================================================
-
- //
- // clObject isn't too useful, but here it is all the same.
- //
- #define clObject 1
- #define kAnyClass '****'
-
- //
- // Most classes would use the ImplementClassData macro here;
- // TObject cannot use it, though, because it has no superclass.
- //
- #ifdef DEBUG
-
- TClassData TObject::fClassData = { "TObject", kAnyClass, clObject, sizeof(TObject), nil, {0, nil} };
-
- #else
-
- TClassData TObject::fClassData = { kAnyClass, clObject, sizeof(TObject), nil, {0, nil} };
-
- #endif
-
-
- //========================================================================================
- // CLASS TObject
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // TObject::~TObject:
- //----------------------------------------------------------------------------------------
- TObject::~TObject()
- {
- // Stub
- }
-
- //----------------------------------------------------------------------------------------
- // TObject::ClassData
- //----------------------------------------------------------------------------------------
- const TClassData* TObject::ClassData() const
- {
- return &fClassData;
- } // TObject::ClassData
-
- //----------------------------------------------------------------------------------------
- // TObject::Clone:
- //----------------------------------------------------------------------------------------
- TObject* TObject::Clone()
- {
- UInt32 newObjectSize = this->ObjectSize();
-
- TObject* clonedObject = (TObject*) new char[newObjectSize];
-
- //
- // We should really use operator=
- //
- CopyMemory((Ptr)this, (Ptr)clonedObject, newObjectSize);
- clonedObject->CloneOwnedObjects();
-
- return clonedObject;
- } // TObject::Clone
-
- //----------------------------------------------------------------------------------------
- // TObject::CloneOwnedObjects
- //
- // This is where derived classes have a chance to clone their
- // owned objects after being cloned themselves. This method is
- // called from TObject::Clone, and should not be called from anywhere
- // else.
- //----------------------------------------------------------------------------------------
- void TObject::CloneOwnedObjects()
- {
- } // TObject::CloneOwnedObjects
-
- //----------------------------------------------------------------------------------------
- // TObject::SortOrder:
- //----------------------------------------------------------------------------------------
- Boolean TObject::SortOrder(const TObject* object, void*) const
- {
- return (*this < *object);
- }
-
- //----------------------------------------------------------------------------------------
- // TObject::Key:
- //----------------------------------------------------------------------------------------
- SInt32 TObject::Key() const
- {
- return 0;
- }
-
- //----------------------------------------------------------------------------------------
- // operator==:
- //----------------------------------------------------------------------------------------
- Boolean operator==(const TObject& first, const TObject& second)
- {
- return (first.Key() == second.Key());
- }
-
- //----------------------------------------------------------------------------------------
- // operator<:
- //----------------------------------------------------------------------------------------
- Boolean operator<(const TObject& first, const TObject& second)
- {
- return (first.Key() < second.Key());
- }
-
- #ifdef DEBUG
-
- //----------------------------------------------------------------------------------------
- // TObject::SetupObjectDebugInfo
- //
- // We cannot set up the debugging information in the constructor because the object's
- // vtable will _always_ point at TObject in TObject::TObject. We have to wait for
- // post-constructor time so that we can get the class data from the most-derived object.
- //----------------------------------------------------------------------------------------
- void TObject::SetupObjectDebugInfo()
- {
- fClassDataPtr = ClassData();
- fSignature = ClassData()->ObjectClass();
- }
-
- //----------------------------------------------------------------------------------------
- // TObject::ClassID
- //
- // Inlined in non-debug builds
- //----------------------------------------------------------------------------------------
- SInt32 TObject::ClassID() const
- {
- ((TObject*)this)->SetupObjectDebugInfo();
-
- return ClassData()->ClassID();
- }
-
- //----------------------------------------------------------------------------------------
- // TObject::ObjectSize
- //
- // Inlined in non-debug builds
- //----------------------------------------------------------------------------------------
- UInt32 TObject::ObjectSize() const
- {
- ((TObject*)this)->SetupObjectDebugInfo();
-
- return ClassData()->ObjectSize();
- }
-
- #endif
-
- //----------------------------------------------------------------------------------------
- // TObject::DerivedFrom:
- //----------------------------------------------------------------------------------------
- Boolean TObject::DerivedFrom(SInt32 cast) const
- {
- const TClassData* cd = this->ClassData();
-
- #ifdef DEBUG
- ((TObject*)this)->SetupObjectDebugInfo();
- #endif
-
- while (cd != nil)
- {
- if (cd->ClassID() == cast)
- return true;
- else
- cd = cd->SuperClass();
- }
-
- return false;
- }
-
- //----------------------------------------------------------------------------------------
- // TObject::RegisterForDeleteNotification
- //----------------------------------------------------------------------------------------
- Boolean TObject::RegisterForDeleteNotification(TCanReceiveDeleteNotification*, Boolean /*shouldRegister*/)
- {
- return false;
- }
-
- //----------------------------------------------------------------------------------------
- // TObject::FirstBehaviorOfClass:
- //----------------------------------------------------------------------------------------
- TBehavior* TObject::FirstBehaviorOfClass(SInt32 classID) const
- {
- TBehavior* behavior = this->FirstBehavior();
-
- if((behavior != nil) && (behavior->DerivedFrom(classID) == false))
- behavior = behavior->NextBehaviorOfClass(classID);
-
- return behavior;
- }
-
- //----------------------------------------------------------------------------------------
- // TObject::AddBehavior:
- //----------------------------------------------------------------------------------------
- void TObject::AddBehavior(TBehavior* behavior)
- {
- //
- // make sure we don't already have the behavior so we don't get into circular references
- //
- Boolean inList = false;
-
- TBehavior* nextBehavior = this->FirstBehavior();
- while (nextBehavior != nil && inList == false)
- {
- if (behavior == nextBehavior)
- inList = true;
- else
- nextBehavior = nextBehavior->NextBehavior();
- }
- if (!inList)
- {
- behavior->SetNextBehavior(this->FirstBehavior(), this);
- this->SetFirstBehavior(behavior);
- }
- } // TObject::AddBehavior
-
- //----------------------------------------------------------------------------------------
- // TObject::RemoveBehavior:
- //----------------------------------------------------------------------------------------
- void TObject::RemoveBehavior(TBehavior* behaviorToRemove)
- {
- // jtr 8/23/94 test the first behavior separately from the rest of the list
- // because changing the first behavior changes the value of fBehavior
- TBehavior* firstBehavior = this->FirstBehavior();
- if(firstBehavior != nil)
- {
- if (firstBehavior == behaviorToRemove)
- {
- this->SetFirstBehavior(firstBehavior->NextBehavior());
- behaviorToRemove->SetNextBehavior(nil, nil);
- }
- else
- {
- Boolean removed = false;
-
- TBehavior* previousBehavior = firstBehavior; // the first behavior
- TBehavior* currentBehavior = firstBehavior->NextBehavior(); // the second behavior
- while (currentBehavior != nil && !removed)
- {
- if (currentBehavior == behaviorToRemove)
- {
- previousBehavior->SetNextBehavior(currentBehavior->NextBehavior(), this);
- behaviorToRemove->SetNextBehavior(nil, nil);
- removed = true;
- }
- else
- currentBehavior = currentBehavior->NextBehavior();
- }
- }
- }
- } // TObject::RemoveBehavior
-
- //----------------------------------------------------------------------------------------
- // TObject::FirstBehavior
- //----------------------------------------------------------------------------------------
- TBehavior* TObject::FirstBehavior() const
- {
- return nil;
- }
-
- //----------------------------------------------------------------------------------------
- // TObject::SetFirstBehavior
- //----------------------------------------------------------------------------------------
- void TObject::SetFirstBehavior(TBehavior* /*firstBehavior*/)
- {
- FailErr(eUnimplemented);
- }
-
-
-